//
//  HR_PickViewController.m
//  HR_PickKit
//
//  Created by KK on 2017/7/20.
//  Copyright © 2017年 Mac. All rights reserved.
//

#import "HR_PickViewController.h"
#import "HR_Macro.h"
#import "HR_PickCollectionViewCell.h"
#import "HR_originalImageViewController.h"

NSString *const indentifier = @"HR_PickCollectionViewCell";
static CGFloat minimumLineSpacing = 4.0f;
static CGFloat minimumInteritemSpacing = 4.0f;
static CGFloat widthSpace = 25.0f;

@interface HR_PickViewController ()<
UICollectionViewDelegate,
UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout
>
/** 图片 */
@property (weak, nonatomic) IBOutlet UICollectionView *pickCollectionView;
@property (strong, nonatomic) NSMutableArray *photoAssetMArr;



@end

@implementation HR_PickViewController

#pragma mark - ----------   Lifecycle（生命周期） ----------
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


#pragma mark - ---------- Private Methods（私有方法） ----------

#pragma mark initliaze data(初始化数据)

#pragma mark config control（布局控件）
- (void)configUI{
    [self configTitle];
    [self configrRightAndLeftBarButtonItem];
    [self registerCollcetionViewCell];

}
- (void)configTitle{
    self.navigationItem.title = @"所有照片";
}
- (void)configrRightAndLeftBarButtonItem{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancelAction)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
}
- (void)registerCollcetionViewCell{
    [self.pickCollectionView registerNib:[UINib nibWithNibName:@"HR_PickCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:indentifier];
}
#pragma mark networkRequest (网络请求)

#pragma mark actions （点击事件）
- (void)cancelAction{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark IBActions （点击事件xib）

#pragma mark - ---------- Public Methods（公有方法） ----------

#pragma mark self declare （本类声明）

#pragma mark override super （重写父类）

#pragma mark setter （重写set方法）

#pragma mark - ---------- Protocol Methods（代理方法） ----------

#pragma mark - CollectionViewDelegate && Delegate && FlowLayout  -

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return  self.assetFetchResult.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HR_PickCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:indentifier forIndexPath:indexPath];
    [cell updatePickCell:self.assetFetchResult[indexPath.item]];
    return cell;
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat width = (SCREEN_WIDTH - widthSpace) / 4;
    return CGSizeMake(width,width);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(5.0f, 5.0f, 5.0f, 5.0f);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collecjtionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return minimumLineSpacing;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return minimumInteritemSpacing;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    HR_originalImageViewController *original = [[HR_originalImageViewController alloc] initWithPHFetchResult:self.assetFetchResult currentIndex:indexPath.item];
    [self presentViewController:original animated:YES completion:nil];
}

#pragma mark - ---------- Lazy Loading（懒加载） ----------

- (NSMutableArray *)photoAssetMArr{
    if (!_photoAssetMArr) {
        _photoAssetMArr = [NSMutableArray array];
    }
    return _photoAssetMArr;
}







@end
